package com.pogodin.wave;

import java.util.*;

public class LetterCount {

    private int [] arr;

    public static void main(String [] a){
        String word = "hello111";
        Map<Character, Integer> charToAmountMap = new HashMap<Character, Integer>();
        LetterCount letterCount = new LetterCount();

        int [] letters = letterCount.countLetters(word);
        for (int i = 0; i < letters.length; i++) {
            if(letters[i] > 0){
                System.out.println((char)('a' + i) + " " + letters[i]);
            }
        }

        System.out.println("nextOne");

        Map<Character, Integer> lettersMap = letterCount.countCharacter(word);
        for (Character character : lettersMap.keySet()) {
            System.out.println(character + " "+ lettersMap.get(character));
        }
    }

    public int[] countLetters(String word){
        int [] letters = new int[26];
        Arrays.fill(letters, 0);
        word = word.toLowerCase();

        for(int i = 0; i < word.length(); i++){
            char currentChar = word.charAt(i);
            if(currentChar <= 'z' && currentChar >= 'a'){
                letters[currentChar - 'a']++;
            }
        }

        return letters;
    }

    public Map<Character, Integer> countCharacter(String word){
        Map<Character, Integer> lettersUsingMap = new TreeMap<Character, Integer>();

        word = word.toLowerCase();

        for(int i = 0; i < word.length(); i++){
            char currentChar = word.charAt(i);
            Integer numberOfUsing = lettersUsingMap.get(currentChar);
            int number = numberOfUsing == null ? 0 : numberOfUsing;
            lettersUsingMap.put(currentChar,1 + number);
        }
        return lettersUsingMap;
    }
}
